package com.zhang.ssmschoolshop.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhang.ssmschoolshop.entity.Activity;
import com.zhang.ssmschoolshop.entity.ActivityExample;
import com.zhang.ssmschoolshop.entity.Admin;
import com.zhang.ssmschoolshop.entity.Goods;
import com.zhang.ssmschoolshop.service.ActivityService;
import com.zhang.ssmschoolshop.service.GoodsService;
import com.zhang.ssmschoolshop.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/admin/activity")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    @Autowired
    private GoodsService goodsService;

    // 公共的登录验证方法
    private boolean isAdminLoggedIn(HttpSession session) {
        Admin admin = (Admin) session.getAttribute("admin");
        return admin != null;
    }

    /**
     * 显示所有活动页面（分页）
     */
    @GetMapping("/show")
    public String showActivity(@RequestParam(value = "page", defaultValue = "1") Integer pn, 
                               Model model, 
                               HttpSession session) {

        // 登录验证
        if (!isAdminLoggedIn(session)) {
            return "redirect:/admin/login";
        }

        // 设置分页参数，每页显示10条数据
        PageHelper.startPage(pn, 10);

        // 获取所有活动信息
        ActivityExample activityExample = new ActivityExample();
        activityExample.or();  // 空查询条件，表示查询所有

        List<Activity> activityList = activityService.getAllActivity(activityExample);

        // 包装分页信息
        PageInfo<Activity> pageInfo = new PageInfo<>(activityList, 5);
        model.addAttribute("pageInfo", pageInfo);

        return "activity";
    }

    /**
     * 获取所有活动数据（JSON）
     */
    @GetMapping("/showjson")
    @ResponseBody
    public Msg showActivityJson(@RequestParam(value = "page", defaultValue = "1") Integer pn, 
                                HttpSession session) {

        // 登录验证
        if (!isAdminLoggedIn(session)) {
            return Msg.fail("请先登录");
        }

        // 设置分页参数
        PageHelper.startPage(pn, 10);

        // 获取所有活动信息
        ActivityExample activityExample = new ActivityExample();
        activityExample.or();  // 空查询条件，表示查询所有

        List<Activity> activityList = activityService.getAllActivity(activityExample);

        return Msg.success("获取活动信息成功").add("activity", activityList);
    }

    /**
     * 跳转到添加活动页面
     */
    @GetMapping("/add")
    public String showAddActivity(HttpSession session) {
        // 登录验证
        if (!isAdminLoggedIn(session)) {
            return "redirect:/admin/login";
        }
        return "addActivity";
    }

    /**
     * 添加活动结果
     */
    @PostMapping("/addResult")
    public String addActivity(@ModelAttribute Activity activity) {
        // 插入活动
        activityService.insertActivitySelective(activity);
        return "redirect:/admin/activity/show";
    }

    /**
     * 更新活动
     */
    @PostMapping("/update")
    @ResponseBody
    public Msg updateActivity(@RequestParam Integer goodsid, 
                              @RequestParam Integer activityid, 
                              HttpSession session) {

        // 登录验证
        if (!isAdminLoggedIn(session)) {
            return Msg.fail("请先登录");
        }

        // 更新商品与活动的关联
        Goods goods = new Goods();
        goods.setActivityid(activityid);
        goods.setGoodsid(goodsid);
        goodsService.updateGoodsById(goods);

        return Msg.success("更新商品活动成功");
    }

    /**
     * 删除活动
     */
    @PostMapping("/delete")
    public String deleteActivity(@RequestParam Integer activityid, HttpSession session) {
        // 登录验证
        if (!isAdminLoggedIn(session)) {
            return "redirect:/admin/login";
        }

        // 删除活动
        activityService.deleteByActivityId(activityid);
        return "redirect:/admin/activity/show";
    }
}
